var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/businessComponent/html-editor/index.tsx
var html_editor_exports = {};
__export(html_editor_exports, {
  HtmlEditor: () => HtmlEditor
});
module.exports = __toCommonJS(html_editor_exports);
var import_editor_for_react = require("@wangeditor/editor-for-react");
var import_style = require("@wangeditor/editor/dist/css/style.css");
var import_react = require("react");
var import_util = require("../../functionalComponent/table/util");
var import_util2 = require("../../util");
var import_service = require("../Attachment/service");
var import_util3 = require("../Attachment/util");
var import_type = require("./type");
var splitKey = "__jkd__";
function HtmlEditor(props) {
  const { value, fileTypeCode, editorProps, onChange } = props;
  const cursorIdRef = (0, import_react.useRef)(0);
  const [cursorId, setCursorId] = (0, import_react.useState)(null);
  const [editor, setEditor] = (0, import_react.useState)(null);
  const [insertedImage, setInsertedImage] = (0, import_react.useState)([]);
  const [html, setHtml] = (0, import_react.useState)(value);
  (0, import_react.useEffect)(() => {
    if (!!value) {
      const tmp = value.split(splitKey);
      if (tmp.length > 1) {
        setHtml(tmp.slice(1).join(splitKey));
      } else {
        setHtml(value);
      }
    }
  }, [value]);
  const addTokenToRul = (html2) => {
    const regex = /src="([^"]+)/g;
    const token = import_util2.iup.getUser().uCode;
    return html2.replace(regex, function() {
      return `${arguments[0]}?token=${token}`;
    });
  };
  const onchangeEditor = (value2) => {
    if (value2 !== "<p><br></p>") {
      !!onChange && onChange(`${cursorIdRef.current || ""}${splitKey}${value2}`);
    } else {
      !!onChange && onChange("");
    }
    !onChange && setHtml(value2);
  };
  const customParseImageSrc = (src) => {
    if ((src == null ? void 0 : src.indexOf("http")) !== 0) {
      return `http://${src}`;
    }
    return src;
  };
  const setInsertedImageFun = (imageNode) => {
    setInsertedImage([...insertedImage, imageNode]);
  };
  const user = import_util.util.getUser() || {};
  const editorConfig = {
    // TS 语法
    MENU_CONF: {
      uploadImage: {
        server: "/iup-file/tripartite/rich-text-upload",
        // server: '/api/upload-image',
        fieldName: "custom-field-name",
        allowedFileTypes: [],
        headers: {
          "User-Token": user.accessToken
        },
        meta: {
          otherKey: "yyy"
        },
        onBeforeUpload(file) {
          console.log("file", file);
          return file;
        },
        // 单个文件上传成功之后
        onSuccess(file, res) {
          console.log(`${file.name} 上传成功`, res);
        },
        // 单个文件上传失败
        onFailed(file, res) {
          console.log(`${file.name} 上传失败`, res);
        },
        // 上传错误，或者触发 timeout 超时
        onError(file, err, res) {
          console.log(`${file.name} 上传出错`, err, res);
        },
        async customUpload(file, insertFn) {
          var _a, _b;
          const { name, size, type } = file;
          const chunkSize = 5242880;
          const currentChunkSize = chunkSize;
          const md5 = await (0, import_util3.getFileMd5)(file);
          const params = {
            chunkNumber: 1,
            chunkSize: 5242880,
            currentChunkSize,
            totalChunks: 1,
            totalSize: size,
            fileName: name,
            md5,
            file,
            busiType: fileTypeCode || "iupfile-type",
            cursorId: "",
            // todo 以下选填
            ctlgId: ""
          };
          if (!fileTypeCode) {
            import_util2.iup.message("缺少fileTypeCode", "warning");
            return;
          }
          const res = await (0, import_service.richUploadFile)(params);
          if (res.code !== 0) {
            return;
          }
          const userToken = import_util2.iup.getUser().accessToken;
          const cursorId2 = (_a = res == null ? void 0 : res.data) == null ? void 0 : _a.cursorId;
          const url = ((_b = res == null ? void 0 : res.data) == null ? void 0 : _b.previewUrl) + "&User-Token=" + userToken;
          const href = "";
          const alt = "test";
          cursorIdRef.current = cursorId2;
          insertFn(url, alt, href);
        }
      },
      insertImage: {
        onInsertedImage(imageNode) {
          if (imageNode == null)
            return;
          const { src, alt, url, href } = imageNode;
          setInsertedImageFun(imageNode);
          console.log("setInsertedImageFun", imageNode);
        },
        parseImageSrc: customParseImageSrc
        // 也支持 async 函数
      }
    }
  };
  (0, import_react.useEffect)(() => {
    const formImageList = editor == null ? void 0 : editor.getElemsByType("image");
  }, [editor == null ? void 0 : editor.getElemsByType("image"), insertedImage]);
  (0, import_react.useEffect)(() => {
    return () => {
      if (editor == null)
        return;
      editor.destroy();
      setEditor(null);
    };
  }, [editor]);
  return /* @__PURE__ */ React.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement("div", { style: { border: "1px solid #ccc", zIndex: 100 } }, /* @__PURE__ */ React.createElement(
    import_editor_for_react.Toolbar,
    {
      editor,
      defaultConfig: import_type.toolbarConfig,
      mode: "default",
      style: { borderBottom: "1px solid #ccc" }
    }
  ), /* @__PURE__ */ React.createElement(
    import_editor_for_react.Editor,
    {
      defaultConfig: editorConfig,
      value: html,
      onCreated: setEditor,
      onChange: (editor2) => {
        onchangeEditor(editor2.getHtml());
      },
      mode: "default",
      style: { height: "500px", overflowY: "hidden" }
    }
  )));
}
(0, import_util2.registerComponent)({ HtmlEditor });
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  HtmlEditor
});
